<template>
  <div class="container">
    <!-- 文章分类栏 -->
    <van-tabs @change="changeTab" v-model="activeIndex" swipeable>
      <van-tab v-for="(item,i) in myChannels" :key="item.id" :title="item.name">
        <!-- 可滚动内容区域 -->
        <article-list :ref="'alist' + i" :channelId="item.id" />
      </van-tab>
    </van-tabs>
    <!-- 右侧图标 -->
    <span class="bar_btn" @click="showEditChannel=true">
      <van-icon name="wap-nav"></van-icon>
    </span>
    <!-- ActionSheet组件 -->
    <channel-edit v-model="showEditChannel" :myChannels="myChannels" :activeIndex.sync="activeIndex"></channel-edit>
  </div>
</template>

<script>
import { getMyChannels } from '@/api/channel' // 频道栏
import ArticleList from './components/articleList.vue'
// 导入频道组件
import ChannelEdit from './components/channel-edit'
export default {
  name: 'home-index',
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      // 我的频道列表(推荐频道默认拥有第一个频道)
      myChannels: [],
      // 标签栏默认索引0
      activeIndex: 0,
      // 频道开关
      showEditChannel: false
    }
  },
  // 处理refresh_token失效，回跳缓存不刷新问题
  watch: {
    '$store.state.user.token' () {
      console.log('refresh')
      this.getChannels()
    }
  },
  created () {
    this.getChannels()
  },
  // 被缓存组件再次被打开时执行
  activated () {
    // console.log('被缓存组件再次被打开时执行')
    this.$refs['alist' + this.activeIndex] && this.$refs['alist' + this.activeIndex][0].scrollList()
  },
  methods: {
    // 获取频道列表
    async getChannels () {
      const { data } = await getMyChannels()
      this.myChannels = data.channels
    },

    // 切换标签栏
    changeTab (i) {
      // console.log(i, this.$refs)
      // 调用当前频道子组件滚动方法
      this.$refs['alist' + i] && this.$refs['alist' + i][0].scrollList()
    }
  }
}
</script>

<style scoped lang='less'>
.van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  // 自定义tab样式
  /deep/ .van-tabs__wrap {
    height: 36px;
    padding-right: 36px;
    .van-tab {
      line-height: 36px;
    }
    .van-tabs__line {
      background-color: #3296fa;
      height: 2px;
    }
  }
  /deep/ .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  /deep/ .van-tab__pane {
    height: 100%;
    // 控制列表区域可滚动
    .scroll-wrapper {
      height: 100%;
      overflow-y: auto;
    }
  }
}
.bar_btn {
  width: 36px;
  height: 35px;
  position: absolute;
  top: 0;
  right: 0;
  font-size: 20px;
  text-align: center;
  line-height: 40px;
  box-shadow: -6px -6px 8px #ddd;
}
</style>
